package com.zkzx.company.api.service.Workbench;

import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.driver.vo.DriverStatisticsVO;
import com.zkzx.common.freight.vo.OrderStatisticsVO;
import com.zkzx.common.truck.vo.TruckStatisticsVO;
import com.zkzx.company.api.client.company.CompanyClient;
import com.zkzx.company.api.client.order.OrderClient;
import com.zkzx.company.api.controller.workbench.vo.WorkbenchVO;
import com.zkzx.company.api.mapper.entity.Announcement;
import com.zkzx.company.api.service.message.MessageService;
import com.zkzx.company.api.service.sitemail.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/18/13:11
 * @Description:
 */
@Slf4j
@Service
public class WorkbenchService {

    @Autowired
    private CompanyClient companyClient;

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private OrderClient orderClient;



    public WorkbenchVO getById(Integer companyId) {

        // 获取企业详细 先查询全量 后期有性能问题在优化
        CompanyVO companyVO = companyClient.getById(companyId);

        Announcement announcement = announcementService.getByCompany();

        TruckStatisticsVO truckStatistics = companyClient.getTruckStatistics(companyId);

        DriverStatisticsVO driverStatistics = companyClient.getDriverStatistics(companyId);


        OrderStatisticsVO orderStatistics = orderClient.getOrderStatistics(companyId);


        int unreadMessageNum = messageService.getUnreadMessageNum(companyId);

        return WorkbenchVO.builder()
                .companyName(companyVO.getName())
                .balanceAmount(BigDecimal.ZERO)
                .announcement(Optional.ofNullable(announcement).map(Announcement::getContext).orElse(null))
                .unreadMessageNum(unreadMessageNum)
                .usableDriver(driverStatistics.getUsableDriver())
                .runDriver(driverStatistics.getRunDriver())
                .usableTruck(truckStatistics.getUsableTruck())
                .runTruck(truckStatistics.getRunTruck())
                .orderNum(orderStatistics.getOrderNum())
                .invoiceOrderNum(orderStatistics.getInvoiceOrderNum())
                .unpaidOrderNum(orderStatistics.getUnpaidOrderNum())
                .errorOrderNum(orderStatistics.getErrorOrderNum())
                .build();
    }
}
